Fork me on GitHub

minikube搭建一个小型k8s集群

minikube可以比较快速地在笔记本电脑上搭建一个小型的k8s集群,集于virtualbox,不适合大型生产环境,但作为初学者入门学习k8s还是很方便的。记录一下我在macOs的minikube搭建过程。

搭建过程

安装方法见 https://brew.sh/ 但根据它的命令行下载方式,可能下载报错443

解决办法:修改hosts,增加域名解析

首先去https://www.ipaddress.com/查找域名对应的IP

1
2
3
sudo vim /etc/hosts
然后添加:
199.232.68.133 githubusercontent.com

  • 由于brew自己的源下载实在是太慢了,修改为清华源
1
2
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
  • brew install kubectl
1
2
3
4
zsh completions have been installed to:
/usr/local/share/zsh/site-functions
==> Summary
🍺 /usr/local/Cellar/kubernetes-cli/1.18.5: 232 files, 49.2MB
  • 安装minikube
1
curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.11.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
  • 获取阿里云镜像加速器地址
1
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
  • 执行minikube start,参数里用上刚刚获取到的加速器地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --registry-mirror=https://8tzxcfw0.mirror.aliyuncs.com --vm-driver=virtualbox
😄 Darwin 10.15.5 上的 minikube v1.11.0
✨ 根据用户配置使用 virtualbox 驱动程序
✅ 正在使用镜像存储库 registry.cn-hangzhou.aliyuncs.com/google_containers
👍 Starting control plane node minikube in cluster minikube
🔥 Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
🐳 正在 Docker 19.03.8 中准备 Kubernetes v1.18.3…
> kubeadm.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubectl.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubelet.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
> kubeadm: 37.97 MiB / 37.97 MiB [-----------] 100.00% 224.60 KiB p/s 2m54s
> kubectl: 41.99 MiB / 41.99 MiB [-----------] 100.00% 202.98 KiB p/s 3m32s
> kubelet: 108.04 MiB / 108.04 MiB [---------] 100.00% 193.39 KiB p/s 9m32s
🔎 Verifying Kubernetes components...
🌟 Enabled addons: default-storageclass, storage-provisioner
🏄 完成!kubectl 已经配置至 "minikube"

可验证:

1
2
3
4
5
6
7
tangmingyu@MINGYUTANG-MB1 ~ % minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

  • 开启minikube自带的web管理界面
1
2
3
4
5
6
tangmingyu@MINGYUTANG-MB1 ~ % minikube dashboard
🔌 正在开启 dashboard ...
🤔 正在验证 dashboard 运行情况 ...
🚀 Launching proxy ...
🤔 正在验证 proxy 运行状况 ...
🎉 Opening http://127.0.0.1:57097/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...

minikube实践

常用命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
minikube status
minikube start --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --registry-mirror=https://8tzxcfw0.mirror.aliyuncs.com --vm-driver=virtualbox
minikube stop
kubectl get pods -A
kubectl get nodes
kubectl get namespaces
kubectl get deployment
kubectl delete deployment xxx

创建一个nginx应用

创建方式可以选择直接传YAML文件,或者是通过表单创建(最终也还是帮我们转化为YAML的方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80

创建成功后:

1
2
3
4
5
tangmingyu@MINGYUTANG-MB1 ~ % kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-5bf87f5f59-266zn 1/1 Running 0 3m20s
nginx-deployment-5bf87f5f59-6vwpm 1/1 Running 0 65s
nginx-deployment-5bf87f5f59-vszjh 1/1 Running 0 3m20s

创建一个ubuntu应用

创建nginx应用很简单,不需要加command。但如果想创建一个ubuntu的pod,不加command就会报错:Back-off restarting failed container

解决方法:https://blog.csdn.net/weixin_42551369/article/details/102545957

成功创建的YAML文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
name: ubuntu18-deployment
spec:
selector:
matchLabels:
app: ubuntu18
replicas: 1
template:
metadata:
labels:
app: ubuntu18
spec:
containers:
- name: ubuntu
image: ubuntu:18.04
command: ["/bin/bash","-c","--"]
args: ["while true; do sleep 30; done;"]
ports:
- containerPort: 80
-------------本文结束感谢您的阅读-------------